import { _decorator, Node, Label, UITransform, Size, Color } from 'cc';
import { BasePanel } from './base-panel';
const { ccclass, property } = _decorator;

/**
 * 带标题栏的面板组件
 */
@ccclass('TitledPanel')
export class TitledPanel extends BasePanel {
    /** 标题文本 */
    @property
    protected title: string = 'Panel';

    /** 标题栏高度 */
    @property
    protected titleBarHeight: number = 40;

    /** 标题栏颜色 */
    @property
    protected titleBarColor: Color = new Color(64, 64, 64, 255);

    /** 标题文本颜色 */
    @property
    protected titleTextColor: Color = Color.WHITE;

    /** 标题栏节点 */
    private titleBar: Node | null = null;

    /** 标题文本组件 */
    private titleLabel: Label | null = null;

    /**
     * 生命周期：加载时
     */
    protected override onLoad(): void {
        super.onLoad();
        this.componentName = 'TitledPanel';
        this.createTitleBar();
    }

    /**
     * 创建标题栏
     */
    private createTitleBar(): void {
        // 创建标题栏节点
        this.titleBar = new Node('TitleBar');
        this.node.addChild(this.titleBar);

        // 设置标题栏大小和位置
        const panelSize = this.node.getComponent(UITransform)?.contentSize;
        if (panelSize) {
            const titleBarTransform = this.titleBar.addComponent(UITransform);
            titleBarTransform.setContentSize(new Size(panelSize.width, this.titleBarHeight));
            this.titleBar.position.set(0, panelSize.height / 2 - this.titleBarHeight / 2, 0);
        }

        // 创建标题文本节点
        const titleNode = new Node('Title');
        this.titleBar.addChild(titleNode);
        this.titleLabel = titleNode.addComponent(Label);
        this.titleLabel.string = this.title;
        this.titleLabel.color = this.titleTextColor;

        // 设置标题文本位置
        const titleTransform = titleNode.getComponent(UITransform);
        if (titleTransform) {
            titleTransform.setContentSize(new Size(200, this.titleBarHeight));
        }
    }

    /**
     * 设置标题
     * @param title 标题文本
     */
    public setTitle(title: string): void {
        this.title = title;
        if (this.titleLabel) {
            this.titleLabel.string = title;
        }
    }

    /**
     * 获取标题
     * @returns 标题文本
     */
    public getTitle(): string {
        return this.title;
    }

    /**
     * 设置标题栏颜色
     * @param color 颜色
     */
    public setTitleBarColor(color: Color): void {
        this.titleBarColor = color;
        // TODO: 更新标题栏背景色
    }

    /**
     * 设置标题文本颜色
     * @param color 颜色
     */
    public setTitleTextColor(color: Color): void {
        this.titleTextColor = color;
        if (this.titleLabel) {
            this.titleLabel.color = color;
        }
    }
} 